﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMS.Data;
using CMS.Service.EnumHelper;
using CMS.Service.Repository;

namespace CMSApplication.Areas.Administration.Models
{
    public class WorkModel
    {
        public int Id { get; set; }
        public string BillNum { get; set; }
        public string Note { get; set; }
        public string Content { get; set; }
        public double Cost { get; set; }
        public double? Collected { get; set; }
        public int? Status { get; set; }
        public int? AssignCustomer { get; set; }
        public int? AssignStaff { get; set; }
        public DateTime? AssignOn { get; set; }
        public int? CreatedBy { get; set; }
        public DateTime? CreatedOn { get; set; }
        public int? LastUpdatedBy { get; set; }
        public DateTime? LastUpdatedOn { get; set; }

        public string StatusName { get; set; }
        public string CustomerName { get; set; }
        public string CustomerAddr { get; set; }
        public string CustomerPhone { get; set; }
        public string StaffName { get; set; }
        public SelectList SLStatus { get; set; }
        public SelectList SLAssignStaff { get; set; }

        public bool IsContinue { get; set; }

        public void GetDataForSL()
        {
            var user_srv = new UserRepositry();

            var status_lst = new List<KeyValuePair<int, string>>();
            status_lst.Add(new KeyValuePair<int, string>(0, "- - Chọn - -"));
            status_lst.AddRange(EnumHelper.Get_Enum_Wrk_Status());

            var assign_staff_lst = new List<KeyValuePair<int, string>>();
            assign_staff_lst.Add(new KeyValuePair<int, string>(0, "- - Chọn - -"));
            foreach (var item in user_srv.List().Where(x => x.Status == true))
            {
                assign_staff_lst.Add(new KeyValuePair<int, string>(item.Id, string.Format("{0} - {1}", item.Id.ToString().PadLeft(4, '0'), item.FullName)));
            }
            
            this.SLStatus = new SelectList(status_lst, "Key", "Value");
            this.SLAssignStaff = new SelectList(assign_staff_lst, "Key", "Value");
        }

        public static void MapFrom(WorkModel model, ref Work entity)
        {
            entity.Id = model.Id;
            entity.BillNum = model.BillNum;
            entity.Note = model.Note != null ? model.Note : "";
            entity.Content = model.Content;
            entity.Cost = model.Cost;
            entity.Collected = model.Collected;
            entity.Status = model.Status;
            entity.AssignCustomer = model.AssignCustomer;
            entity.AssignStaff = model.AssignStaff;
            entity.AssignOn = model.AssignOn;
            entity.CreatedBy = model.CreatedBy;
            entity.CreatedOn = model.CreatedOn;
            entity.LastUpdatedBy = model.LastUpdatedBy;
            entity.LastUpdatedOn = model.LastUpdatedOn;
        }

        public static void MapFrom(Work entity, ref WorkModel model)
        {
            model.Id = entity.Id;
            model.BillNum = entity.BillNum;
            model.Note = entity.Note;
            model.Content = entity.Content;
            model.Cost = entity.Cost;
            model.Collected = entity.Collected.HasValue ? entity.Collected.Value : 0;
            model.Status = entity.Status;
            model.AssignCustomer = entity.AssignCustomer;
            model.AssignStaff = entity.AssignStaff;
            model.AssignOn = entity.AssignOn;
            model.CreatedBy = entity.CreatedBy;
            model.CreatedOn = entity.CreatedOn;
            model.LastUpdatedBy = entity.LastUpdatedBy;
            model.LastUpdatedOn = entity.LastUpdatedOn;
        }
    }
}
